package data.missions;

import data.LinkedList;
import data.Map;
import data.Player;
import data.tileables.Base;
import data.tileables.Unit;

public class Heal extends Mission
{
	private Map map;
	private Base base;
	private Player player;
	
	public Heal(Map m, Base b, Player p)
	{
		this.map=m;
		this.base=b;
		this.player=p;
		this.displayName = "Heal";
	}
	
	public int execute()
	{
		LinkedList<int[]> list=this.map.getArea(this.base.getX(), this.base.getY(), 0);
		list.setToFirst();
		int totalDamage=0;
		for(int i=0;i<list.getSize();i++)
		{
			LinkedList<Unit> u=this.map.getUnitsAt(list.peekCurrent()[0],list.peekCurrent()[1]);
			if(u!=null)
			{
				u.setToFirst();
				Unit currentU=u.peekCurrent();
				if(currentU.getPlayerID()==this.base.getPlayerID())
				{
					
					for(int j=0;j<u.getSize();j++)
					{
						int[] stats=currentU.getStats();
						int health=stats[0];
						int maxHealth=stats[1];
						int damage=maxHealth-health;
						totalDamage+=damage;
						if(damage>0)
						{
							currentU.changeHealth(base.getHealRate());
						}
						u.next();
						currentU=u.peekCurrent();
					}
				}
			}
			list.next();
		}
		
		if(totalDamage>0)
		{
			return 2;
		}
		else
		{
			return 1;
		}
	}
	 
}